﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;

public partial class Controls_ControlLoaiMatHangContent : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_Data();
        }
    }

    /// <summary>
    /// Phuong thuc load du lieu vao Control<Literal></Literal>
    /// </summary>
    protected void Bind_Data()
    {
        int ToTalRows = 0;
        string WhereClause = "1=1";
        string WhereClause1 = "1=1";
        string strHTML = "";
        TList<LoaiMatHang> Tlist_LoaiMatHang = DataRepository.LoaiMatHangProvider.GetPaged(WhereClause, "Id DESC", 0, 0, out ToTalRows);
        if (Tlist_LoaiMatHang.Count > 0)
        {
            for (int i = 0; i < Tlist_LoaiMatHang.Count; i++)
            {
                strHTML += "<div class=\"sub-loai-mat-hang\">";
                strHTML += "<div class=\"title-sub-mat-hang\">";
                strHTML += "<span>" + Tlist_LoaiMatHang[i].TenMatHang + "</span>";

                // --------------------------------------------------------------
                // Lay duoc Id cua LoaiMathang sau do lay danh sach sub mat hang
                // cua Loai mat hang do roi hien thi du lieu.
                WhereClause = "1=1";
                WhereClause += " and IdLoaiMatHang = " + Tlist_LoaiMatHang[i].Id;
                TList<SubMatHang> Tlist_SubMathang = DataRepository.SubMatHangProvider.GetPaged(WhereClause, "Id DESC", 0, 0, out ToTalRows);

                for (int j = 0; j < Tlist_SubMathang.Count; j++)
                {
                    WhereClause1 = "1=1";
                    WhereClause1 += " and IdSubMatHang = " + Tlist_SubMathang[j].Id;
                    TList<MatHang> TList_MatHang = DataRepository.MatHangProvider.GetPaged(WhereClause1, "NgayCapNhat DESC", 0, 6, out ToTalRows);
                    if (TList_MatHang.Count > 0)
                    {
                        strHTML += "<div>";
                        strHTML += "<ul>";
                        for (int k = 0; k < TList_MatHang.Count; k++)
                        {
                            strHTML += "<li>";
                            strHTML += "<a href='" + TList_MatHang[k].Id + "_" + TList_MatHang[k].IdSubMatHang + "_" + GetTieuDe(TList_MatHang[k].TieuDe) + "_DoanhNghiep.aspx'>";
                            strHTML += "<img title='" + TList_MatHang[k].TomTat + "' src='Upload/MatHang/" + TList_MatHang[k].AnhDaiDien + "'></img>";
                            strHTML += "<label title=' " + TList_MatHang[k].TomTat + "'>" + formatString(TList_MatHang[k].TieuDe, 15) + "</label></a></li>";
                        }
                        strHTML += "</ul>";
                        strHTML += "</div>";
                    }

                }
                // --------------------------------------------------------------
                strHTML += "</div>";
                strHTML += "</div>";
                literalSubMatHang.Text = strHTML;
            }
        }
    }

    /// <summary>
    /// Phuong thuc chuyen tieu de co dau thanh khong dau de dua vao link
    /// </summary>
    /// <param name="Char"></param>
    /// <returns></returns>
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    /// <summary>
    /// Phuong thuc cat chuoi, de lay so ky tu can hien thi
    /// </summary>
    /// <param name="str"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }
}